package org.feng.test;

import lombok.extern.slf4j.Slf4j;
import org.feng.clog.AbstractProcessorTemplate;
import org.feng.clog.LogRecordContext;
import org.feng.clog.ProcessorContext;
import org.feng.clog.annotation.CLog;
import org.feng.clog.annotation.ProcessorBean;
import org.feng.clog.enums.ActionTypeEnum;
import org.feng.clog.enums.ModuleEnum;
import org.feng.clog.utils.StringUtil;
import org.springframework.stereotype.Service;

/**
 * 创建订单处理器
 *
 * @author feng
 */
@Slf4j
@Service
@ProcessorBean(action = ActionTypeEnum.ORDER_CREATE)
public class OrderCreateProcessor extends AbstractProcessorTemplate<OrderCreateReq, Boolean> {

    @Override
    protected void init(ProcessorContext<OrderCreateReq> context) {
        preHandleReq(context.getData());
    }

    @Override
    @CLog(template = "测试日志记录，{testK1}", module = ModuleEnum.ORDER, actionType = ActionTypeEnum.ORDER_CREATE,
            actionIdEl = "{#context.data.orderNum}", ext = "{JacksonUtil.toJSONString(#context.data)}"
    )
    public Boolean handle(ProcessorContext<OrderCreateReq> context) {

        LogRecordContext.put("testK1", "3wewd2");

        OrderCreateReq orderCreateReq = context.getData();
        log.info("处理--创建订单{}", orderCreateReq.getOrderNum());

        return true;
    }

    @Override
    protected void after(ProcessorContext<OrderCreateReq> context, Boolean result) {
        // todo 后置操作
    }

    private void preHandleReq(OrderCreateReq req) {
        // todo 参数校验

        // 例如校验参数
        if (StringUtil.isBlank(req.getOrderNum())) {
            throw new IllegalArgumentException("订单号不能为空");
        }
    }
}
